package com.aion.jirc.irc.wrappers;

/**
 * Created by IntelliJ IDEA.
 * Date: 11-02-18
 * Time: 09:13
 *
 * @author Aion
 */
public class User {

    private String realName;
    private String address;
    private String name;

    private long idleTime;

    public User(String name) {
        this.name = name;
    }

    public User(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public User(String name, String address, String realName) {
        this.name = name;
        this.address = address;
        this.realName = realName;
    }

    /**
     * Gets the address of this user.
     *
     * @return the address of this user or an empty <code>String</code>
     */
    public String getAddress() {
        if (address == null) {
            return "";
        }
        return address;
    }

    /**
     * Gets the idle time of this user.
     *
     * @return the idle time
     */
    public long getIdleTime() {
        return idleTime;
    }

    /**
     * Gets the name of this user.
     *
     * @return the name of this user
     */
    public String getName() {
        return name;
    }

    /**
     * Gets the real name of this user.
     *
     * @return the real name of this user or an empty <code>String</code>
     */
    public String getRealName() {
        if (realName == null) {
            return "";
        }
        return realName;
    }

    /**
     * Checks whether or not this user is admin on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is admin on
     * @return <tt>true</tt> if this user is admin; otherwise <tt>false</tt>
     */
    public boolean isAdmin(Channel channel) {
        return isThis(channel.getAdmins());
    }

    /**
     * Checks whether or not this user is founder on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is founder on
     * @return <tt>true</tt> if this user is founder; otherwise <tt>false</tt>
     */
    public boolean isFounder(Channel channel) {
        return isThis(channel.getFounders());
    }

    /**
     * Checks whether or not this user is halfop on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is half-operator on
     * @return <tt>true</tt> if this user is halfop; otherwise <tt>false</tt>
     */
    public boolean isHalfoperator(Channel channel) {
        return isThis(channel.getHalfoperators());
    }

    /**
     * Checks whether or not this user is on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is on
     * @return <tt>true</tt> if this user is on the channel; otherwise <tt>false</tt>
     */
    public boolean isOnChannel(Channel channel) {
        return isThis(channel.getUsers());
    }

    /**
     * Checks whether or not this user is operator on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is operator on
     * @return <tt>true</tt> if this user is operator; otherwise <tt>false</tt>
     */
    public boolean isOperator(Channel channel) {
        return isThis(channel.getOperators());
    }

    /**
     * Checks whether or not this user is regular on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is regular on
     * @return <tt>true</tt> if this user is regular; otherwise <tt>false</tt>
     */
    public boolean isRegular(Channel channel) {
        return isThis(channel.getRegulars());
    }

    /**
     * Checks whether or not this user is voice on the given channel.
     *
     * @param channel
     *          the <code>Channel</code> instance to check if this user is voice on
     * @return <tt>true</tt> if this user is voice; otherwise <tt>false</tt>
     */
    public boolean isVoice(Channel channel) {
        return isThis(channel.getVoices());
    }

    private boolean isThis(User[] users) {
        if (users != null) {
            for (User user : users) {
                if (user.equals(this)) {
                    return true;
                }
            }
        }
        return false;
    }

}
